//----- Studetn ID : MC110202704
//----- Assignment : 3
//----- Spring 2013

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <algorithm>


using namespace std;
void pause() __attribute__((destructor)); /* Function Declaration, You Cannot Declare Attributes Without This */
void pause(){ getch(); }

class Node
{
    public:
        int get(){ return nodeValue; };
        void set(int prmVal){ this->nodeValue = prmVal; };
        Node* getNext(){ return nextNode; };
        Node* getPrev(){ return prevNode; }; 
        void setNext(Node *nextNode){ this->nextNode = nextNode; };
        void setPrev(Node* prevNode){ this->prevNode = prevNode; };
           
    private:
        int nodeValue; // it stores the actual value of the element
        Node* nextNode; // this points to the next node
        Node* prevNode; // this points to the previous node
};


class List
{
    public:
       List();
       void add(Node* addObject);
       Node* get();
       int length();
       bool next();
       void start();
       void remove();
//       friend void traverse(List list);
           
    private:
        Node* headNode;
        Node* currentNode;
        Node* lastCurrentNode;
        int size;
};

    //constructor
    List::List()
    {
        headNode = new Node();
        headNode->setNext(NULL);
        currentNode = NULL;
        size = 0;
    }
    
    void List::add(Node* addObject)
    {
         Node *newNode = addObject;
         //newNode->setArray(addObject);
         if (currentNode != NULL)
         {
            newNode->setNext(currentNode->getNext());
            currentNode->setNext(newNode);
            lastCurrentNode = currentNode;
            currentNode = newNode;
         }
         else
         {
             newNode->setNext(NULL);
             headNode->setNext(newNode);
             lastCurrentNode = headNode;
             currentNode = newNode;
         }
         size++;
    }
    // position currentNode and lastCurrentNode at first element
    void List::start() {
        lastCurrentNode = headNode;
        currentNode = headNode;
    }
    /* get() class method */
    Node* List::get()
    {
        if (currentNode != NULL)
           return currentNode;
    }
    /* Size() class method */
    int List::length()
    {
        return size;
    }
    /* next() class method */
    bool List::next()
    {
        if (currentNode == NULL) return false;
        lastCurrentNode = currentNode;
        currentNode = currentNode->getNext();
        if (currentNode == NULL || size == 0)
            return false;
        else
            return true;
    }
    void List::remove() {
        if( currentNode != NULL && currentNode != headNode) {
            lastCurrentNode->setNext(currentNode->getNext());
            delete currentNode;
            currentNode = lastCurrentNode;
            size--;
        }
    }
//    /* Friend function to traverse linked list */
//    void traverse(List list)
//    {
//        Node* savedCurrentNode = list.currentNode;
//        list.currentNode = list.headNode;
//        for(int i = 1; list.next(); i++)
//        {
//            cout << "\n Element " << i << " " << list.get();
//        }
//        list.currentNode = savedCurrentNode;
//    }


List* lstNodes = new List();

void getInputs(int NoOfNodes){
    int curValue = 0;
    int j;
    bool isValidInput;
    
    for (int nCount=0;nCount<NoOfNodes;nCount++){
        Node* newNode = new Node();
        cout << "Enter the Name of Node " << endl;
        cin >> newNode->nodeName;
        for (int i=0;i<6;i++){
            isValidInput = false;
            j=i+1;
            while (!isValidInput){
                cout << "Enter value " << j << endl;
                cin >> curValue;
                if (cin.fail()){
                   cin.clear();
                   cin.ignore();
                   cout << "invalid input. Please enter a number!" << endl;
                }else{
                   isValidInput=true;
                   for (int k=0;k<i;k++){
                       if (newNode->getValue(k)==curValue){
                           cin.clear();
                           cin.ignore();
                           cout << "Duplicate value within one node is not allowed." << endl;
                           isValidInput=false;
                       }
                   }
                   newNode->setValue(i, curValue);
                }
            }
        }
        lstNodes->add(newNode);
        cout << "\ntotal is " << newNode->getTotal() << endl;
    }
}    

int main(int argc, char *argv[])
{
    int NoOfNodes;
    int j;
    cout << "How many nodes you want to create?" << endl;
    cin >> NoOfNodes;
    getInputs(NoOfNodes);
    lstNodes->start();
    for (int nCount=0;nCount<NoOfNodes;nCount++){
        lstNodes->next();
        j=nCount+1;
        cout << "total of node " << j << " is " << lstNodes->get()->getTotal() << endl;
    }
    
    return EXIT_SUCCESS;
}
